﻿using Microsoft.Practices.Unity;
using PicStore.BLL.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PicStore.DAL;
using PicStore.Model;

namespace PicStore.BLL.Impl
{
    public class CategoryService : BaseService, ICategoryService
    {
        [Dependency]
        public IUserService UserService { get; set; }

        private CategoryInfo BuildItemInfo(CategoryItem item)
        {
            var categoryItem = new CategoryInfo()
            {
                Id = item.CategoryItemId,
                CategoryName = item.Category.CategoryName,
                CategoryId = item.CategoryId,
                CreateTime = item.CreateTime,
                ItemName = item.CategoryItemName,
                ItemValue = item.CategoryItemValue,
                Description = item.Description,
                Status = item.Status,
                CreaterId = item.OperaterId,
                LastUpdateTime = item.LastUpdateTime,
                CreaterName = UserService.GetUser(item.OperaterId).UserName,
            };

            return categoryItem;
        }

        public List<CategoryInfo> GetAllItems(string categoryName)
        {
            var itemList = (from item in dbContext.CategoryItem
                            join catetgory in dbContext.Category on item.CategoryId equals catetgory.CategoryId
                            where item.CategoryItemName.Contains(categoryName)
                                  && item.Status == (int)CategoryItemEnum.正常
                                  && catetgory.ShowForAdmin == true
                            select item).ToList();

            List<CategoryInfo> list = new List<CategoryInfo>();
            foreach (var t in itemList)
            {
                list.Add(BuildItemInfo(t));
            }
            return list;
        }

        public List<CategoryInfo> GetItemsByCategoryId(int categoryId)
        {
            var itemList = (from c in dbContext.CategoryItem
                            where c.CategoryId == categoryId && c.Status == (int) CategoryItemEnum.正常
                            select c).ToList();

            List<CategoryInfo> list = new List<CategoryInfo>();
            foreach (var t in itemList)
            {
                list.Add(BuildItemInfo(t));
            }
            return list;
        }

        public List<string> GetItemValuesByCategoryId(int categoryId)
        {
            return (from c in dbContext.CategoryItem
                    where c.CategoryId == categoryId && c.Status == (int)CategoryItemEnum.正常
                    select c.CategoryItemValue).ToList();
        }

        public List<string> GetOtherItemValues(int categoryId, int itemId)
        {
            return (from c in dbContext.CategoryItem
                    where c.CategoryId == categoryId && c.Status == (int)CategoryItemEnum.正常 && c.CategoryItemId != itemId
                    select c.CategoryItemValue).ToList();
        }

        public List<CategoryInfo> GetBaseCategoryListForAdmin()
        {
            var list = (from c in dbContext.Category
                        where c.ShowForAdmin == true
                        select c).ToList();

            List<CategoryInfo> result = new List<CategoryInfo>();
            foreach (var t in list)
            {
                CategoryInfo info = new CategoryInfo();
                info.Id = t.CategoryId;
                info.CategoryId = t.CategoryId;
                info.CategoryName = t.CategoryName;
                result.Add(info);
            }

            return result;
        }

        public int CreateCategoryItem(int categoryId, string itemName, string itemValue, int createrId, string description)
        {
            CategoryItem item = new CategoryItem();
            item.CategoryId = categoryId;
            item.CategoryItemName = itemName;
            item.CategoryItemValue = itemValue;
            item.CreateTime = DateTime.Now;
            item.LastUpdateTime = DateTime.Now;
            item.OperaterId = createrId;
            item.Description = description;
            item.Status = (int) CategoryItemEnum.正常;

            dbContext.CategoryItem.InsertOnSubmit(item);
            dbContext.SubmitChanges();

            return item.CategoryItemId;
        }

        public string GetItemName(int categoryId, string itemValue)
        {
            return (from c in dbContext.CategoryItem
                    where c.CategoryId == categoryId && c.Status == (int)CategoryItemEnum.正常 && c.CategoryItemValue == itemValue
                    select c.CategoryItemName).FirstOrDefault();
        }

        public CategoryInfo GetCategoryItemById(int itemId)
        {
            var item = (from c in dbContext.CategoryItem
                        where c.CategoryItemId == itemId && c.Status == (int)CategoryItemEnum.正常
                        select c).FirstOrDefault();

            CategoryInfo result = null;
            if (item != null)
            {
                result = BuildItemInfo(item);
            }

            return result;
        }

        private CategoryItem GetItemById(int itemId)
        {
            return (from item in dbContext.CategoryItem
                    where item.CategoryItemId == itemId
                    select item).FirstOrDefault();
        }

        public void UpdateCateoryItem(int itemId, string itemName, string itemValue, int categoryId, int operatorId, string description)
        {
            var item = GetItemById(itemId);
            if (item != null)
            {
                item.CategoryId = categoryId;
                item.CategoryItemName = itemName;
                item.CategoryItemValue = itemValue;
                item.LastUpdateTime = DateTime.Now;
                item.OperaterId = operatorId;
                item.Description = description;

                dbContext.SubmitChanges();
            }
        }

        public void DeleteCateoryItem(int itemId)
        {
            var item = GetItemById(itemId);
            if (item != null)
            {
                item.Status = (int) CategoryItemEnum.已删除;

                dbContext.SubmitChanges();
            }
        }

        public string GetDescriptionOfItems(string value, BaseCategoryEnum categoryEnum)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat("{0} include: ", categoryEnum.ToString());
            List<string> array = value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
            foreach (var place in array)
            {
                var itemName = GetItemName((int)categoryEnum, place);
                builder.Append(itemName).Append(",");
            }
            builder.AppendLine();
            return builder.ToString();
        }
    }
}
